package com.jyb.qiyefuwu.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.jyb.qiyefuwu.api.EnterpriseServiceAPIManager;
import com.jyb.qiyefuwu.entity.NeedFollowListBean;
import com.sgb.net.entity.BaseEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class NeedFollowModel extends BaseViewModel {
    public MutableLiveData<List<NeedFollowListBean>> followList = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<String> show_toast = new MutableLiveData<>();
    public MutableLiveData<String> attentionNo = new MutableLiveData<>();
    public MutableLiveData<String> followStatus = new MutableLiveData<>();
    private int page = 1;
    private Map map = new HashMap();
    private Map gzMap = new HashMap();

    public NeedFollowModel(@NonNull Application application) {
        super(application);
    }

    //需求类型 0全部 1商标 2专利 3版权 4 公司 5服务（需求列表特有）
    public void getData(int type, boolean isLodMore) {
        page = isLodMore ? page + 1 : 1;
        map.put("type", type);
        map.put("limit", 10);
        map.put("page", page);
        map.put("attentionNo", attentionNo.getValue());
        getNeedList();
    }


    public void getNeedList() {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().getDemandHallListXQ(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedFollowListBean>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedFollowListBean>>>> call, Response<BaseEntity<ListResponse<List<NeedFollowListBean>>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    followList.postValue(response.body().getData().getList());
                } else {
                    show_toast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedFollowListBean>>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    public void saveAttention(int type,String id,String dataId) {
        gzMap.put("id",id);
        gzMap.put("dataId", dataId);
        gzMap.put("type", type);
        gzMap.put("attentionNot", 1);
        gzMap.put("attentionTime", System.currentTimeMillis());
        gzMap.put("attentionNo", attentionNo.getValue());
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().saveAttention(gzMap).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    followStatus.postValue("ok");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }
}
